<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html lang="zh-CN">
<script src="js/jquery-3.6.0.js"></script>
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>用户登录与注册</title>
  <style>
    body {
      font-family: Arial, sans-serif;
      background-color: #f5f5f5;
      margin: 0;
      padding: 20px;
    }
    .container {
      max-width: 400px;
      margin: 0 auto;
      background: white;
      padding: 20px;
      border-radius: 8px;
      box-shadow: 0 0 10px rgba(0,0,0,0.1);
    }
    h2 {
      text-align: center;
      color: #333;
    }
    .form-group {
      margin-bottom: 15px;
    }
    label {
      display: block;
      margin-bottom: 5px;
      font-weight: bold;
    }
    input[type="text"],
    input[type="password"],
    input[type="email"] {
      width: 100%;
      padding: 10px;
      border: 1px solid #ddd;
      border-radius: 4px;
      box-sizing: border-box;
    }
    button {
      width: 100%;
      padding: 10px;
      background-color: #4CAF50;
      color: white;
      border: none;
      border-radius: 4px;
      cursor: pointer;
      font-size: 16px;
    }
    button:hover {
      background-color: #45a049;
    }
    .switch-form {
      text-align: center;
      margin-top: 15px;
    }
    .switch-form a {
      color: #4CAF50;
      text-decoration: none;
      cursor: pointer;
    }
    .switch-form a:hover {
      text-decoration: underline;
    }
    .hidden {
      display: none;
    }
    .error-message {
      color: red;
      font-size: 14px;
      margin-top: 5px;
    }
  </style>
</head>
<body>
<div class="container">
  <!-- 登录表单 -->
  <div id="login-form">
    <h2>用户登录</h2>
    <form id="login">
      <div class="form-group">
        <label for="login-username">用户名</label>
        <input type="text" id="login-username" name="username" required>
      </div>
      <div class="form-group">
        <label for="login-password">密码</label>
        <input type="password" id="login-password" name="password" required>
      </div>
      <div id="login-error" class="error-message"></div>
      <button type="submit">登录</button>
    </form>
    <div class="switch-form">
      还没有账号？<a id="show-register">立即注册</a>
    </div>
  </div>

  <!-- 注册表单 -->
  <div id="register-form" class="hidden">
    <h2>用户注册</h2>
    <form id="register">
      <div class="form-group">
        <label for="register-username">用户名</label>
        <input type="text" id="register-username" name="username" required>
        <div id="username-error" class="error-message"></div>
      </div>
      <div class="form-group">
        <label for="register-password">密码</label>
        <input type="password" id="register-password" name="password" required>
        <div id="password-error" class="error-message"></div>
      </div>
      <div class="form-group">
        <label for="register-email">邮箱</label>
        <input type="email" id="register-email" name="email" required>
        <div id="email-error" class="error-message"></div>
      </div>
      <div id="register-error" class="error-message"></div>
      <button type="submit">注册</button>
    </form>
    <div class="switch-form">
      已有账号？<a id="show-login">立即登录</a>
    </div>
  </div>
</div>

<script>
  // 切换登录和注册表单
  document.getElementById('show-register').addEventListener('click', function() {
    document.getElementById('login-form').classList.add('hidden');
    document.getElementById('register-form').classList.remove('hidden');
  });

  document.getElementById('show-login').addEventListener('click', function() {
    document.getElementById('register-form').classList.add('hidden');
    document.getElementById('login-form').classList.remove('hidden');
  });

  // 登录表单提交
  document.getElementById('login').addEventListener('submit', function(e) {
    e.preventDefault();
    const username = document.getElementById('login-username').value;
    const password = document.getElementById('login-password').value;
    let TF = true;
    $.ajax({
      url: '/login',
      type: 'POST',
      dataType: 'json',
      data: {
        'username': username,
        'password': password
      },
      success: function(response) {
        console.log(response)
        TF = response.success;
      }
    })

    // 这里应该是AJAX请求到后端验证登录
    // 模拟登录成功
    if(TF) {
      alert('登录成功！');

      // 实际应用中这里应该跳转到用户主页
      window.location.href = '/index.jsp';
    } else {
      document.getElementById('login-error').textContent = '用户名或密码不能为空';
    }
  });

  // 注册表单提交
  document.getElementById('register').addEventListener('submit', function(e) {
    e.preventDefault();
    const username = document.getElementById('register-username').value;
    const password = document.getElementById('register-password').value;
    const email = document.getElementById('register-email').value;

    // 简单的客户端验证
    let isValid = true;

    // 验证用户名
    if(username.length < 4) {
      document.getElementById('username-error').textContent = '用户名至少需要4个字符';
      isValid = false;
    } else {
      document.getElementById('username-error').textContent = '';
    }

    // 验证密码
    if(password.length < 6) {
      document.getElementById('password-error').textContent = '密码至少需要6个字符';
      isValid = false;
    } else {
      document.getElementById('password-error').textContent = '';
    }

    // 验证邮箱
    if(!email.includes('@') || !email.includes('.')) {
      document.getElementById('email-error').textContent = '请输入有效的邮箱地址';
      isValid = false;
    } else {
      document.getElementById('email-error').textContent = '';
    }

    if(isValid) {
      // 这里应该是AJAX请求到后端进行注册
      // 模拟注册成功
      alert('注册成功！请检查您的邮箱激活账户。');
      // 重置表单
      this.reset();
      // 切换到登录表单
      document.getElementById('register-form').classList.add('hidden');
      document.getElementById('login-form').classList.remove('hidden');

    }
  });
</script>
</body>
</html>